$(function () {


    function query_un_pay_order_info_list() {
        api.lorder.query_pay_order_info_list({charge_status: '0,1,3',page_index:html_data.no_page_index+1}).then(function (data) {
            // html_data.un_pay_order_list = data.result.list;
            html_data.no_page_index = data.result.page_index;
            html_data.no_page_count = data.result.page_count;
            _(data.result.list).each(function (item) {
                html_data.un_pay_order_list.push(item);
                item['goods_num'] = 0;
                _(item.goods_order_list).each(function (order) {
                    _(order.goods_order_item_list).each(function (goods) {
                        item['goods_num'] += goods.num;

                    });
                });

            });
        });
    }

    function query_pay_order_info_list() {
        api.lorder.query_goods_order_list({status: '1', goods_status: '0,1,2,3',page_index:html_data.pay_page_index+1}).then(function (data) {
            // html_data.goods_order_list = data.result.list;
            html_data.pay_page_index = data.result.page_index;
            html_data.pay_page_count = data.result.page_count;
            _(data.result.list).each(function (item) {
                html_data.goods_order_list.push(item);
                item['goods_num'] = 0;
                _(item.goods_order_item_list).each(function (goods) {
                    item['goods_num'] += goods.num;
                });
            });
        });
    }

    function query_goods_order_list() {
        api.lorder.query_finish_goods_order_list({page_index:html_data.over_page_index+1}).then(function (data) {
            // html_data.pay_order_list = data.result.list;
            html_data.over_page_index = data.result.page_index;
            html_data.over_page_count = data.result.page_count;
            _(data.result.list).each(function (item) {
                html_data.pay_order_list.push(item);
                item['goods_num'] = 0;
                _(item.goods_order_item_list).each(function (goods) {
                    item['goods_num'] += goods.num;
                });
            });
        });
    }
    function query_dispute_goods_order_list() {
        api.lorder.query_order_dispute_by_sys_list({order_id:GetQueryString("order_id"),page_index:html_data.over_page_index+1}).then(function (data) {
            // html_data.pay_order_list = data.result.list;
            html_data.dispute_page_index = data.result.page_index;
            html_data.dispute_page_count = data.result.page_count;
            _(data.result.list).each(function (item) {
                html_data.dispute_order_list.push(item);
            });
        });
    }

    function query_live_type_list() {
        api.lprogram.query_live_type_list().then(function (data) {
            program_data.live_type_list.push({id:0, name:"推荐", checked:true});
            _(data.result.list).each(function (item) {
                if(item.parent_id>0){
                    program_data.live_type_list.push(item);
                }
            });

        });
    }


    var html_data = {
        baseConfig: baseConfig,
        un_pay_order_list: [],
        goods_order_list: [],
        pay_order_list: [],
        dispute_order_list: [],
        no_page_index:0,
        pay_page_index:0,
        over_page_index:0,
        no_page_count:0,
        pay_page_count:0,
        over_page_count:0,
        dispute_page_index:0,
        dispute_page_count:0,
    };
    var app = new Vue({
        el: '#orderlist',
        data: html_data,
        methods: {
            price_filter: price_filter,
            query_un_pay_order_info_list: query_un_pay_order_info_list,
            query_pay_order_info_list: query_pay_order_info_list,
            query_goods_order_list: query_goods_order_list,
            query_dispute_goods_order_list: query_dispute_goods_order_list,
            make_goods_order: function (goods_order) {
                api.lorder.confirm_goods_received({goods_order_id: goods_order.id}).then(function (data) {
                    goods_order.goods_status = data.result.goods_status;
                    Magiclamp.mcAlert("提示", data.message);
                });
            },

            open_payorder: function (payorder_id) {
                location.href = 'pay.html?payorder_id=' + payorder_id;
            },
            open_order_info: function (order_id) {
                location.href = 'user_goods_orders.html?goods_order_id=' + order_id;
            },
            open_dispute_order_info: function (order_id) {
                location.href = 'user_dispute_goods_orders.html?dispute_id=' + order_id;
            },
            delete_payorder: function (payorder) {
                Magiclamp.mcConfirm("提示", "确认删除订单么？", null, function () {
                    api.lorder.delete_order({pay_order_id: payorder.id}).then(function (data) {
                        Magiclamp.mcAlert("提示", data.message);
                        payorder.is_active = false;

                        html_data.un_pay_order_list = _(html_data.un_pay_order_list).filter(function (item) {
                            return item.is_active;
                        });
                    });
                });

            },
            dispute_goods_order: function (goods_order) {
                Magiclamp.mcConfirm("提示", "确认投诉订单么？", null, function () {
                    api.lorder.dispute_goods_order({order_id: goods_order.id}).then(function (data) {
                        Magiclamp.mcAlert("提示", data.message);
                        goods_order.dispute_step = 1
                    })
                });
            },
            shengyudays: function (goods_order) {
                if (goods_order.goods_status == 4 || goods_order.goods_status == 0 || goods_order.goods_status == 1) {
                    return "";
                }
                if (goods_order.dispute_step == 0) {
                    //"received_date" "shipped_date"
                    if (goods_order.goods_status == 2) {
                        var shipped_date = moment();
                        var end_date = moment(goods_order.shipped_date);
                        end_date.add(10, 'days');
                        var days = end_date.diff(shipped_date, 'days');
                        if (days == 0) {
                            return "(剩余：" + end_date.diff(shipped_date, 'hours') + "小时)"
                        } else {
                            return "(剩余：" + days + "天)";
                        }
                    } else if (goods_order.goods_status == 3) {
                        var shipped_date = moment();
                        var end_date = moment(goods_order.received_date);
                        end_date.add(10, 'days');
                        var days = end_date.diff(shipped_date, 'days');
                        if (days == 0) {
                            return "(剩余：" + end_date.diff(shipped_date, 'days') + "小时)"
                        } else {
                            return "(剩余：" + days + "天)";
                        }
                    }

                } else {
                    return "";
                }
            },
            go_order_detail: function (payorder_id) {
                location.href = './order-detail.html?payorder_id=' + payorder_id;
            },
            open_dispute_order:function (dispute_order) {
                location.href = './dispute_order_list.html?order_id=' + dispute_order.order_id;
            },
            page_back: function () {
                baseConfig.page_back();
            }
        }
    });

    html_data.sessionid = GetQueryString('sessionid');
    if (html_data.sessionid) {
        html_data.sessionid_q = "sessionid=" + html_data.sessionid;
        client_storage('sessionid', html_data.sessionid);
    }
    query_dispute_goods_order_list();



});
